<?php

namespace app\store\controller\finance;

use app\common\enum\OrderType as OrderTypeEnum;
use app\common\library\MysicLog;
use app\common\library\wechat\WxPay;
use app\common\service\Order as OrderService;
use app\store\controller\Controller;
use think\Db;


/**
 * 订单信息
 * Class Order
 * @package app\store\controller\statistics
 */
class Large extends Controller
{
    
    /**
     * 用户充值
     * @param $user_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function preparePay()
    {
        $wxapp_id = $this->getWxappId();

        $param = $this->request->param();
        $log = MysicLog::instance();
        $log->setFilename('prepay/large');
        /*if (!isset($param['recharge_money']) || $param['recharge_money'] === '' || $param['recharge_money'] < 0) {
            return $this->renderError('请输入正确的充值金额');
        }*/
        if (!isset($param['recharge_money']) || !is_numeric($param['recharge_money'])||!preg_match("/^[1-9][0-9]*$/" ,$param['recharge_money'])) {
            return ['code'=>0,'msg'=>'请输入正确的充值金额(大于等于5000的正整数)','data'=>[]];
        }
        if (isset($param['recharge_money']) && ($param['recharge_money']<5000)) {
            return ['code'=>0,'msg'=>'充值金额不能小于5000','data'=>[]];
        }
        //获取微信配置
        $wxConfig = db('base_setting')->where(['key'=>'largepay'])->value('values');
        if(!$wxConfig){
            //return $this->renderError('微信配置错误');
            return ['code'=>0,'msg'=>'微信配置错误','data'=>[]];
        }
        $wxConfig = json_decode($wxConfig,true);
        //充值金额
        $recharge_money = $param['recharge_money'];
        $log->info($recharge_money, '充值金额');
        //获取手续费，即实际支付金额
        $pay_price = bcadd($recharge_money/100,0,2);
        $log->info($pay_price, '实际支付金额');
        if($pay_price<=0){
            //return $this->renderError('微信支付金额不能小于0');
            return ['code'=>0,'msg'=>'微信支付金额不能小于或等于0','data'=>[]];
        }
        try {
            $orderData = [
                'order_no' => OrderService::createOrderNo(),
                'pay_price' => $pay_price,
                'recharge_money' => $recharge_money,
                'pay_status' => 10,
                'wxapp_id' => $wxapp_id,
                'create_time' => time(),
                'admin_id' => $this->store['user']['store_user_id'],
            ];
            $log->info($orderData, '订单数据支付金额');
            $orderResult = db('large_order')->insertGetId($orderData);
            $log->info($orderResult, '生成订单状态');
            if(!$orderResult){
                //return $this->renderError('生成订单失败，请重试');
                return ['code'=>0,'msg'=>'生成订单失败，请重试','data'=>[]];
            }
            //返回微信统一下单结果
            $WxPay = new WxPay($wxConfig);
            $wxReturn = $WxPay->unifiedordernative($orderData['order_no'],$orderData['pay_price'],OrderTypeEnum::ADMINPAY);
            $returnData = ['order_no'=>$orderData['order_no'],'url'=>$wxReturn['code_url']];
            //return $this->renderSuccess('支付成功','',$returnData);
            return ['code'=>1,'msg'=>'获取成功','data'=>$returnData];
        } catch(\Exception $e) {
            $log->info(exceptionLogFormat($e), '生成订单失败');
            //return $this->renderError(exceptionLogFormat($e)['msg']);
            return ['code'=>0,'msg'=>exceptionLogFormat($e)['msg'],'data'=>[]];
        }
    }

    /**
     * 微信支付生成二维码
     * @param $user_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getQrcode(){

        $url = input('url');
        if($url){
            require_once WEB_PATH . '../source/extend/phpqrcode/phpqrcode.php';
            $object = new \QRcode();
            echo $object->png($url,false,QR_ECLEVEL_L,8,2,false,0xFFFFFF,0x000000);
        }

    }

    /**
     * 检查订单是否支付
     * @param $user_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function checkPay(){
        $order_no = input('order_no');
        $pay_status = db("large_order")->where(['order_no'=>$order_no])->value("pay_status");
        if($pay_status==20){
            return $this->renderSuccess('支付成功');
        }else{
            return $this->renderError('未支付');
        }
    }
}